unit clsvariacaoitemvenda;

interface

uses Dm, dbtables, biblio;

type
  TVariacaoItemVenda = class(tobject)
  private
{$HINTS OFF}
    ped_item: string;
    pro_codigo: string;
    ped_numero: string;
{$HINTS ON}
  public
    pro_codigo2: string;
    Mostra_Desc: string;
    function Gravar(p_ped_numero, p_ped_item, p_Pro_codigo: string): byte;
    function Recuperar(p_ped_numero, p_ped_numero2, p_ped_item, p_Pro_codigo: string): byte;
    function Recuperar1(p_ped_numero, p_ped_numero2, p_ped_item, p_Pro_codigo2: string): string;
    function Recuperar2(p_ped_numero, p_Ped_numero2, p_ped_item: string): byte;
    function Recuperar_Produto(p_ped_numero, p_Ped_numero2, p_ped_item, P_Pro_codigo, P_Pro_codigo2: string): byte;
    function Excluir(p_ped_numero, p_ped_item, p_Pro_codigo: string): Byte;
    function Excluir1(p_ped_numero, p_ped_item: string): Byte;
    function ExcluirPorPedido(P_ped_1, P_Ped_2: string): Byte;
    procedure Consultar(var p_q: TQuery; P_Ped1, P_Ped2: string);
    procedure Consultar1(var p_q: TQuery; P_Ped1, P_Ped2, Prod, Item: string);
    procedure Consultar2(var p_q: TQuery; P_Ped1, P_Ped2, P_ped_item: string);
    procedure Copiar_Variacao(pedido1, item1, pedido2, item2: string);
  end;

implementation

{ TVariacaoItemVenda }

procedure TVariacaoItemVenda.Consultar(var p_q: TQuery; P_Ped1,
  P_Ped2: string);
begin
  p_q.sql.add('Select * from VariacaoItemVenda where Ped_numero=:K_Ped1 or ped_numero=:K_Ped2');
  P_q.ParamByName('k_ped1').AsString := StrZero(P_Ped1, 9);
  p_q.ParamByName('k_ped2').AsString := StrZero(P_Ped2, 9);
end;

procedure TVariacaoItemVenda.Consultar2(var p_q: TQuery; P_Ped1, P_Ped2,
  P_ped_item: string);
begin
  p_q.Params.clear;
  p_q.sql.clear;
  p_q.sql.add('Select * from VariacaoItemVenda where (Ped_numero=:K_Ped1 or ped_numero=:K_Ped2) and ped_item=:K_Ped_item');
  P_q.ParamByName('k_ped1').AsString := StrZero(P_Ped1, 9);
  p_q.ParamByName('k_ped2').AsString := StrZero(P_Ped2, 9);
  p_q.ParamByName('K_ped_item').AsString := StrZero(P_ped_item, 2);
end;

function TVariacaoItemVenda.Excluir(p_ped_numero, p_ped_item,
  p_Pro_codigo: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM VariacaoItemVenda where ped_numero=:k_ped_numero and ped_item=:k_ped_item and pro_codigo=:K_pro_codigo');
  tq.ParamByName('k_ped_numero').AsString := StrZero(p_ped_numero, 9);
  tq.parambyname('k_ped_item').asstring := StrZero(p_ped_item, 2);
  tq.parambyname('K_pro_codigo').asstring := p_Pro_codigo;
  try
    tq.execSQL;
    Result := 1;
  except
    Result := 0;
  end;
  tq.close;
  tq.Free;

end;

function TVariacaoItemVenda.Excluir1;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM VariacaoItemVenda where ped_numero=:k_ped_numero and ped_item=:k_ped_item');
  tq.ParamByName('k_ped_numero').AsString := StrZero(p_ped_numero, 9);
  tq.parambyname('k_ped_item').asstring := StrZero(p_ped_item, 2);
  try
    tq.execSQL;
    Result := 1;
  except
    Result := 0;
  end;
  tq.close;
  tq.Free;

end;

function TVariacaoItemVenda.ExcluirPorPedido(P_ped_1,
  P_Ped_2: string): Byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.close;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('DELETE FROM VariacaoItemVenda where ped_numero=:K_Ped_1 or Ped_numero=:K_Ped_2');
  tq.ParamByName('k_ped_1').AsString := StrZero(P_ped_1, 9);
  tq.ParamByName('k_ped_2').AsString := StrZero(P_ped_2, 9);
  try
    tq.execSQL;
    Result := 1;
  except
    Result := 0;
  end;
  tq.close;
  tq.Free;
end;

function TVariacaoItemVenda.Gravar(p_ped_numero, p_ped_item,
  p_Pro_codigo: string): byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('SELECT * FROM VariacaoItemVenda where ped_numero=:k_ped_numero and ped_item=:k_ped_item and pro_codigo=:K_pro_codigo');
  tq.ParamByName('k_ped_numero').AsString := StrZero(p_ped_numero, 9);
  tq.parambyname('k_ped_item').asstring := StrZero(p_ped_item, 2);
  tq.parambyname('K_pro_codigo').asstring := p_Pro_codigo;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('INSERT INTO VariacaoItemVenda VALUES(:k_Ped_numero, :K_ped_item, :K_pro_codigo, :k_pro_Codigo2, :k_Mostra_Desc)');
  end else begin
    tq.sql.clear;
    tq.params.clear;
    tq.sql.add('UPDATE VariacaoItemVenda SET pro_codigo2=:k_pro_codigo2,Mostra_Desc=:k_Mostra_Desc where ' +
      'ped_numero=:k_ped_numero and ped_item=:k_ped_item and pro_codigo=:K_pro_codigo');
  end;
  tq.parambyname('k_ped_numero').asstring := StrZero(p_ped_numero, 9);
  tq.parambyname('k_ped_item').asstring := StrZero(p_ped_item, 2);
  tq.parambyname('K_pro_codigo').asstring := (p_Pro_codigo);
  tq.ParamByName('k_pro_codigo2').AsString := (pro_codigo2);
  tq.ParamByName('k_Mostra_Desc').AsString := Mostra_Desc;
  tq.close;
  try
    tq.execsql;
    result := 1;
  except
    Result := 0;
  end;
  tq.close;
  tq.Free;
end;

function TVariacaoItemVenda.Recuperar;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM VariacaoItemVenda where (ped_numero=:k_ped_numero or ' +
    'ped_numero=:k_ped_numero2) and ped_item=:k_ped_item and ' +
    'pro_codigo=:K_pro_codigo');
  tq.Params[0].asstring := StrZero(p_ped_numero, 9);
  tq.Params[1].asstring := StrZero(p_ped_numero2, 9);
  tq.params[2].asstring := StrZero(p_ped_item, 2);
  tq.params[3].asstring := p_Pro_codigo;
  tq.open;
  ped_item := p_ped_item;
  pro_codigo := p_Pro_codigo;
  p_ped_numero := p_ped_numero;
  if tq.eof then begin
    pro_codigo2 := '';
    Mostra_Desc := '';
    Result := 0;
  end else begin
    pro_codigo2 := tq.fieldbyname('pro_codigo2').AsString;
    Mostra_Desc := tq.fieldByName('Mostra_Desc').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;

end;

function TVariacaoItemVenda.Recuperar1;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM VariacaoItemVenda where (ped_numero=:k_ped_numero or ' +
    'ped_numero=:k_ped_numero2) and ped_item=:k_ped_item and ' +
    'pro_codigo2=:K_pro_codigo');
  tq.Params[0].asstring := StrZero(p_ped_numero, 9);
  tq.Params[1].asstring := StrZero(p_ped_numero2, 9);
  tq.params[2].asstring := StrZero(p_ped_item, 2);
  tq.params[3].asstring := p_Pro_codigo2;
  tq.open;
  Result := tq.fieldbyname('pro_codigo').AsString;
  tq.Close;
  tq.Free;
end;

function TVariacaoItemVenda.Recuperar2(p_ped_numero, p_Ped_numero2,
  p_ped_item: string): byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM VariacaoItemVenda where (ped_numero=:k_ped_numero or ped_numero=:K_ped_numero2) and ped_item=:k_ped_item');
  tq.Params[0].asstring := StrZero(p_ped_numero, 9);
  tq.Params[1].asstring := StrZero(p_ped_numero2, 9);
  tq.params[2].asstring := StrZero(p_ped_item, 2);
  tq.open;
  ped_item := p_ped_item;
  p_ped_numero := p_ped_numero;
  if tq.eof then begin
    pro_codigo2 := '';
    Mostra_Desc := '';
    Result := 0;
  end else begin
    pro_codigo2 := tq.fieldbyname('pro_codigo2').AsString;
    Mostra_Desc := tq.fieldByName('Mostra_Desc').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

function TVariacaoItemVenda.Recuperar_Produto(p_ped_numero, p_Ped_numero2,
  p_ped_item, P_Pro_codigo, P_Pro_codigo2: string): byte;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('SELECT * FROM VariacaoItemVenda where (ped_numero=:k_ped_numero or ped_numero=:K_ped_numero2) ' +
    'and ped_item=:k_ped_item and pro_codigo=:K_Pro_codigo and pro_codigo2=:K_pro_codigo2');
  tq.Params[0].asstring := StrZero(p_ped_numero, 9);
  tq.Params[1].asstring := StrZero(p_ped_numero2, 9);
  tq.params[2].asstring := StrZero(p_ped_item, 2);
  tq.params[3].asstring := P_Pro_codigo;
  tq.params[4].asstring := P_Pro_codigo2;
  tq.open;
  ped_item := p_ped_item;
  p_ped_numero := p_ped_numero;
  if tq.eof then begin
    pro_codigo2 := '';
    Mostra_Desc := '';
    Result := 0;
  end else begin
    pro_codigo2 := tq.fieldbyname('pro_codigo2').AsString;
    Mostra_Desc := tq.fieldByName('Mostra_Desc').AsString;
    Result := 1;
  end;
  tq.Close;
  tq.Free;
end;

procedure TVariacaoItemVenda.Copiar_Variacao;
var
  tq: TQuery;
begin
  tq := Tquery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Select * from VariacaoItemVenda where ped_numero=:a and ped_item=:b');
  tq.params[0].asstring := pedido1;
  tq.params[1].asstring := item1;
  tq.open;
  while not tq.eof do begin
    pro_codigo2 := tq.fieldbyname('pro_codigo2').asstring;
    Mostra_Desc := tq.fieldbyname('mostra_desc').asstring;
    Gravar(pedido2, item2, tq.fieldbyname('pro_codigo').asstring);
    tq.next;
  end;
  tq.close;
  tq.free;
end;

procedure TVariacaoItemVenda.Consultar1;
begin
  p_q.sql.add('Select * from VariacaoItemVenda where (Ped_numero=:K_Ped1 ' +
    'or ped_numero=:K_Ped2) and Pro_codigo =:Prod and Ped_item = :It');
  P_q.ParamByName('k_ped1').AsString := StrZero(P_Ped1, 9);
  p_q.ParamByName('k_ped2').AsString := StrZero(P_Ped2, 9);
  p_q.ParamByName('Prod').AsString := Prod;
  p_q.ParamByName('It').AsString := Item;
end;

end.
